package me.zhenqu.model.bo.impl;

import me.zhenqu.model.bo.UserBo;
import me.zhenqu.model.dao.FollowShipDao;
import me.zhenqu.model.pojo.entity.FollowShip;
import me.zhenqu.model.pojo.entity.User;
import me.zhenqu.model.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.LinkedList;
import java.util.List;

@Service("userBo")
public class UserBoImpl implements UserBo {
	
	@Autowired
    UserDao userDao;

    @Autowired
    FollowShipDao followShipDao;

    public void setFollowShipDao(FollowShipDao followShipDao) {
        this.followShipDao = followShipDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save(User stock){
		userDao.save(stock);
	}
	
	public void update(User stock){
		userDao.update(stock);
	}
	
	public void delete(User stock){
		userDao.delete(stock);
	}

    @Override
    public User findByID(Long user_id) {
        return userDao.findByID(user_id);
    }

    @Override
    public User findByNickName(String nick_name) {
        return userDao.findByNickName(nick_name);
    }

    @Override
    public List<Long> loadFollowingUserIds(Long user_id) {
        List<FollowShip> ships = followShipDao.findAllFollowingShips(user_id);
        List<Long> userIds = new LinkedList<Long>();
        if (ships==null||ships.isEmpty()) return userIds;
        for (FollowShip ship: ships)
            userIds.add(ship.getFollow_user_id());
        return userIds;
    }

    @Override
    public List<Long> loadFollowedUserIds(Long user_id) {
        List<FollowShip> ships = followShipDao.findAllFollowedShips(user_id);
        List<Long> userIds = new LinkedList<Long>();
        if (ships==null||ships.isEmpty()) return userIds;
        for (FollowShip ship: ships)
            userIds.add(ship.getUser_id());
        return userIds;
    }

    public void follow(Long user_id, Long targetId){
        FollowShip fs = new FollowShip();
        fs.setUser_id(user_id);
        fs.setCreateTime(System.currentTimeMillis());
        fs.setFollow_user_id(targetId);
        followShipDao.save(fs);
    }

    @Override
    public boolean isFollowing(Long user_id, Long targetId) {
        FollowShip fs = new FollowShip();
        fs.setUser_id(user_id);
        fs.setFollow_user_id(targetId);
        return followShipDao.findBySample(fs);
    }

    @Override
    public boolean isFollowed(Long user_id, Long targetId) {
        FollowShip fs = new FollowShip();
        fs.setUser_id(targetId);
        fs.setFollow_user_id(user_id);
        return followShipDao.findBySample(fs);
    }

    public void unfollow(Long user_id, Long targetId){
        FollowShip fs = new FollowShip();
        fs.setUser_id(user_id);
        fs.setFollow_user_id(targetId);
        followShipDao.delete(fs);
    }

    @Override
    public List<User> search(String keyword) {
        return userDao.search(keyword);
    }

}
